package com.hst.web.base.proxy;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.hst.core.Page;
import com.hst.core.dao.IORDao;
import com.hst.core.dao.ISQLDao;
import com.hst.core.dao.ORQuery;
import com.hst.core.dao.ORQuery.Op;
import com.hst.core.json.JsonUtil;
import com.hst.core.meta.MetaData;
import com.hst.core.meta.proxies.DefaultDataProxy;
import com.hst.web.base.entity.TAccountIdentity;
import com.hst.web.base.entity.TCompanyDept;
import com.hst.web.base.entity.TCompanyEmployee;
import com.hst.web.base.entity.TDeptEmployee;
import com.hst.web.base.service.UserRoleService;
import com.hst.web.base.ui.CompanyEmployeeReview;
import com.hst.web.common.utils.ImageUtils;
import com.hst.web.common.utils.PapersNumberUtils;

public class CompanyEmployeeReviewProxy extends DefaultDataProxy {

	@Autowired
	IORDao dao;

	@Autowired
	ISQLDao sql;

	@SuppressWarnings("unchecked")
	@Override
	public Page<?> list(Map<String, ?> query, int pageIdx) {
		UserRoleService.addCompanyEmpParam((Map<String, Object>) query);
		return sql.list("employee.list-company-employee-review", query, pageIdx, Page.getPageSize());
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<?> list(Map<String, ?> query) {
		UserRoleService.addCompanyEmpParam((Map<String, Object>) query);
		return sql.query("employee.list-company-employee-review", query);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Object save(MetaData data) throws Exception {
		List<ORQuery> querys = new ArrayList<ORQuery>();
		CompanyEmployeeReview meta = (CompanyEmployeeReview) data;
		String employeeCode = meta.getEmployeeCode();
		String dynamicFace = meta.getDynamicFace();
		String companyCode = meta.getCompanyCode();
		
		PapersNumberUtils.validate(meta.getPapersNumber(), meta.getPapersType());

		TCompanyEmployee companyEmployee = dao.get(TCompanyEmployee.class, employeeCode);
		BeanUtils.copyProperties(companyEmployee, meta);
		dao.save(companyEmployee);

		querys.add(new ORQuery(Op.eq, "employeeCode", employeeCode));

		List<TAccountIdentity> accountIdentityList = dao.list(TAccountIdentity.class, querys);
		if (accountIdentityList.size() > 0) {
			TAccountIdentity accountIdentity = accountIdentityList.get(0);
			BeanUtils.copyProperties(accountIdentity, meta);
			dao.save(accountIdentity);
		}

		// 图片旋转处理
		if (!StringUtils.isEmpty(meta.getDeg())) {
			ImageUtils.imgsRotate((Map<String, Integer>) JsonUtil.read(meta.getDeg(), Map.class), meta);
		}

		// 图片反转处理
		if (!StringUtils.isEmpty(meta.getReverse())) {
			ImageUtils.imgsReverse((Map<String, Integer>) JsonUtil.read(meta.getReverse(), Map.class), meta);
		}

		// 图片压缩处理
		ImageUtils.imageCommpress(dynamicFace);
		
		String deptCode = meta.getDeptCode();
		if(StringUtils.isNotEmpty(deptCode)) {
	        if (StringUtils.isNotEmpty(deptCode)) {
	        	TCompanyDept companyDept = dao.get(TCompanyDept.class, deptCode);
	        	if (companyDept != null) {
	        		companyCode = dao.get(TCompanyDept.class, deptCode).getCompanyCode();
	        	}
	        }			
			// 插入部门关系表
			TDeptEmployee deptEmployee;
			deptEmployee = dao.get(TDeptEmployee.class, meta.getLinkId());
			deptEmployee.setEmployeeCode(meta.getEmployeeCode());
			deptEmployee.setDeptCode(deptCode);
			deptEmployee.setCompanyCode(companyCode);
			dao.update(deptEmployee);
		}
		
		return meta;
	}
}
